package com.jwsoft.manager.core.integration.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.base.common.vo.dict.DictFieldRetVO;
import com.bifang.module.base.common.vo.dict.DictQueryVO;
import com.bifang.module.base.common.vo.dict.SelectVO;
import com.bifang.module.base.core.integration.DictIntegration;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.enums.SchoolCategoryEnum;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.convert.Convert;
import java.util.List;
import java.util.stream.Collectors;

import com.bifang.engine.assistant.core.exception.AppException;
import org.springframework.util.ObjectUtils;
import org.springframework.transaction.annotation.Transactional;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.jwsoft.manager.common.vo.eduSyncJhImportBatch.EduSyncJhImportBatchVO;
import com.jwsoft.manager.common.vo.eduSyncJhImportBatch.EduSyncJhImportBatchKeyVO;
import com.jwsoft.manager.common.vo.eduSyncJhImportBatch.EduSyncJhImportBatchQueryVO;
import com.jwsoft.manager.core.integration.EduSyncJhImportBatchIntegration;
import com.jwsoft.manager.core.dao.service.EduSyncJhImportBatchService;
import com.jwsoft.manager.core.dao.model.EduSyncJhImportBatch;
/**
 * 金华市教育同步导入批次表业务实现类
 *
 * @author zhiyuan
 * @since 2024-04-11
 */
@ApiService(funcCode = "syncJhImportBatch", title = "金华市教育同步导入批次表")
public class EduSyncJhImportBatchIntegrationImpl implements EduSyncJhImportBatchIntegration {

    @Autowired
    public EduSyncJhImportBatchService eduSyncJhImportBatchService;

    @Autowired
    private DictIntegration dictIntegration;

    @Autowired
    private EduHelper eduHelper;

    @Override
    @OpApi(funcCode = "syncJhImportBatch0001", title = "金华市教育同步导入批次表分页查询", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE)
    public PageInfo<EduSyncJhImportBatchVO> getList(EduSyncJhImportBatchQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        if (StringUtils.isBlank(vo.getAreaCode())){
            vo.setAreaCode(SessionUtil.getAreaCode());
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        QueryWrapper<EduSyncJhImportBatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(EduSyncJhImportBatch::getAreaCode, vo.getAreaCode())
                .eq(StringUtils.isNotBlank(vo.getImportType()),EduSyncJhImportBatch::getImportType, vo.getImportType())
                .eq(!ObjectUtils.isEmpty(vo.getYear()),EduSyncJhImportBatch::getYear, vo.getYear())
                .orderByDesc(EduSyncJhImportBatch::getCreateTime,EduSyncJhImportBatch::getImportBatchName);
        List<EduSyncJhImportBatch> list = eduSyncJhImportBatchService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduSyncJhImportBatch> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduSyncJhImportBatchVO> resultList = Convert.toList(EduSyncJhImportBatchVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "syncJhImportBatch0002", title = "金华市教育同步导入批次表根据主键查询详情", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE)
    public EduSyncJhImportBatchVO getById(EduSyncJhImportBatchKeyVO vo) {
        EduSyncJhImportBatch entity = eduSyncJhImportBatchService.getById(vo.getImportBatchId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduSyncJhImportBatchVO result = Convert.convert(EduSyncJhImportBatchVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "syncJhImportBatch0003", title = "金华市教育同步导入批次表保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduSyncJhImportBatchVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "syncJhImportBatch0004", title = "金华市教育同步导入批次表新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "syncJhImportBatch0004",
            operationName = "金华市教育同步导入批次表新增",
            dataType = "importBatchId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduSyncJhImportBatchVO add(EduSyncJhImportBatchVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        EduSyncJhImportBatch entity = Convert.convert(EduSyncJhImportBatch.class, vo);
        eduSyncJhImportBatchService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setImportBatchId(entity.getImportBatchId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "syncJhImportBatch0005", title = "金华市教育同步导入批次表修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "syncJhImportBatch0005",
            operationName = "金华市教育同步导入批次表修改",
            dataType = "importBatchId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduSyncJhImportBatchVO edit(EduSyncJhImportBatchVO vo) {
        if (ObjectUtils.isEmpty(vo.getImportBatchId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduSyncJhImportBatch oldEntity = eduSyncJhImportBatchService.getById(vo.getImportBatchId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduSyncJhImportBatch entity = Convert.convert(EduSyncJhImportBatch.class, vo);
        eduSyncJhImportBatchService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "syncJhImportBatch0006", title = "金华市教育同步导入批次表根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "syncJhImportBatch0006",
            operationName = "金华市教育同步导入批次表删除",
            dataType = "importBatchId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduSyncJhImportBatchKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getImportBatchId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduSyncJhImportBatch entity = eduSyncJhImportBatchService.getById(vo.getImportBatchId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduSyncJhImportBatchService.removeById(vo.getImportBatchId());
    }



    @Override
    public List<SelectVO> getSchoolCategory() {
        //得到民族的数据字典列表
        DictQueryVO dictQueryVO = new DictQueryVO();
        dictQueryVO.setDictField("SCHOOLCATEGORY");
        DictFieldRetVO dictField = dictIntegration.getDictField(dictQueryVO);
        List<SelectVO> selectVOList = dictField.getKeyData();
        selectVOList = selectVOList.stream().filter(item -> {
            //过滤幼儿园
            return !SchoolCategoryEnum.KINDERGARTEN.getType().equals(item.getDictKey());
        }).collect(Collectors.toList());
        return selectVOList;
    }

    @Override
    public List<SelectVO> getNationDict() {
        //得到民族的数据字典列表
        DictQueryVO dictQueryVO = new DictQueryVO();
        dictQueryVO.setDictField("NATION");
        DictFieldRetVO dictField = dictIntegration.getDictField(dictQueryVO);
        List<SelectVO> nationDictList = dictField.getKeyData();
        return nationDictList;
    }

    @Override
    public List<SelectVO> getCardTypeDict() {
        DictQueryVO dictQueryVO = new DictQueryVO();
        dictQueryVO.setDictField("CARDTYPE");
        DictFieldRetVO dictField = dictIntegration.getDictField(dictQueryVO);
        List<SelectVO> selectVOList = dictField.getKeyData();
        return selectVOList;
    }

    @Override
    public List<SelectVO> getSexDict() {
        DictQueryVO dictQueryVO = new DictQueryVO();
        dictQueryVO.setDictField("SEXZJ");
        DictFieldRetVO dictField = dictIntegration.getDictField(dictQueryVO);
        List<SelectVO> selectVOList = dictField.getKeyData();
        return selectVOList;
    }

    @Override
    public List<SelectVO> getHouseHoldTypeDict() {
        //得到民族的数据字典列表
        DictQueryVO dictQueryVO = new DictQueryVO();
        dictQueryVO.setDictField("JHHOUSEHOLDTYPE");
        DictFieldRetVO dictField = dictIntegration.getDictField(dictQueryVO);
        List<SelectVO> selectVOList = dictField.getKeyData();
        return selectVOList;
    }

    @Override
    public List<SelectVO> getJHCountyAreaCode() {
        String areaCode = eduHelper.thisAreaCode();
        if (StringUtils.isBlank(areaCode)) {
            throw new AppException("部署行政区划不存在，请联系技术人员进行配置！");
        }
        DictQueryVO dictQueryVO = new DictQueryVO();
        dictQueryVO.setDictField("AREA");
        dictQueryVO.setParentKey(areaCode);
        List<SelectVO> dictCondition = dictIntegration.getDictCondition(dictQueryVO);
        return dictCondition;
    }

    @Override
    public List<SelectVO> getRelationDict() {
        DictQueryVO dictQueryVO = new DictQueryVO();
        dictQueryVO.setDictField("GUARDIANRELATION");
        DictFieldRetVO dictField = dictIntegration.getDictField(dictQueryVO);
        List<SelectVO> selectVOList = dictField.getKeyData();
        return selectVOList;
    }

    @Override
    public List<SelectVO> getApproveStateDict() {
        DictQueryVO dictQueryVO = new DictQueryVO();
        dictQueryVO.setDictField("APPROVESTATE");
        DictFieldRetVO dictField = dictIntegration.getDictField(dictQueryVO);
        List<SelectVO> selectVOList = dictField.getKeyData();
        return selectVOList;
    }

    @Override
    public List<SelectVO> getEnrollStateDict() {
        DictQueryVO dictQueryVO = new DictQueryVO();
        dictQueryVO.setDictField("ENROLLSTATE");
        DictFieldRetVO dictField = dictIntegration.getDictField(dictQueryVO);
        List<SelectVO> selectVOList = dictField.getKeyData();
        return selectVOList;
    }
}
